package com.yy.controller.systemmanager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.common.config.RCConfig;
import com.yy.common.core.AjaxResult;
import com.yy.common.core.HttpStatus;
import com.yy.common.core.model.RestaurantRequestVM;
import com.yy.common.core.model.RestaurantVM;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.utils.JwtHelper;
import com.yy.common.utils.PageHelper;
import com.yy.common.utils.file.FileUploadUtils;
import com.yy.common.utils.file.MimeTypeUtils;
import com.yy.pojo.Restaurant;
import com.yy.service.RestaurantService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * ClassName: ManageRestaurant
 * Package: com.yy.controller.systemmanager
 * Description:
 *
 * @Author yy
 * @Create 2023-12-28 5:44
 * @Version 1.0
 */
@RestController
@RequestMapping("/system-manager/manage-restaurant")
public class ManageRestaurantController {

    @Autowired
    private RestaurantService restaurantService;

    @Autowired
    private JwtHelper jwtHelper;

    @GetMapping("/query")
    public TableDataInfo query(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                               Restaurant restaurant) {
        IPage<Restaurant> page = restaurantService.getRestaurantList(pageNum, pageSize, restaurant);
        Page<RestaurantVM> restaurantVMPage = PageHelper.copyMap(page, r -> {
            RestaurantVM restaurantVM = new RestaurantVM();
            restaurantVM.setRestaurantId(r.getRestaurantId());
            restaurantVM.setRestaurantName(r.getRestaurantName());
            restaurantVM.setRestaurantLocation(r.getRestaurantLocation());
            restaurantVM.setAvatar(r.getAvatar());
            restaurantVM.setStatus(r.getStatus());
            restaurantVM.setOpeningTime(DateUtil.format(r.getOpeningTime(), "HH:mm"));
            restaurantVM.setClosingTime(DateUtil.format(r.getClosingTime(), "HH:mm"));
            return restaurantVM;
        });
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(restaurantVMPage.getRecords());
        rspData.setTotal(restaurantVMPage.getTotal());
        return rspData;
    }

    @GetMapping("{id}")
    public AjaxResult getRestaurantById(@PathVariable("id") Long id) {
        Restaurant restaurant = restaurantService.getById(id);
        RestaurantVM restaurantVM = new RestaurantVM();
        BeanUtil.copyProperties(restaurant, restaurantVM);
        restaurantVM.setOpeningTime(DateUtil.format(restaurant.getOpeningTime(), "HH:mm"));
        restaurantVM.setClosingTime(DateUtil.format(restaurant.getClosingTime(), "HH:mm"));
        return AjaxResult.success(restaurantVM);
    }


    @PostMapping("add")
    public AjaxResult addUser(@RequestPart("restaurantPic") MultipartFile restaurantPic,
                              @RequestPart("restaurant") RestaurantRequestVM restaurantRequestVM) throws Exception {

        Restaurant restaurant = new Restaurant();
        restaurant.setRestaurantName(restaurantRequestVM.getRestaurantName());
        restaurant.setRestaurantLocation(restaurantRequestVM.getRestaurantLocation());
        restaurant.setEmail(restaurantRequestVM.getEmail());
        restaurant.setPhone(restaurantRequestVM.getPhone());
        restaurant.setOpeningTime(DateUtil.parse(restaurantRequestVM.getOpeningTime(), "HH:mm"));
        restaurant.setClosingTime(DateUtil.parse(restaurantRequestVM.getClosingTime(), "HH:mm"));

        if (ObjectUtils.isNotEmpty(restaurantPic) && !restaurantPic.isEmpty()) {
            String avatar = FileUploadUtils.upload(RCConfig.getAvatarPath(), restaurantPic, MimeTypeUtils.IMAGE_EXTENSION);
            restaurant.setAvatar(avatar);
        }

        boolean save = restaurantService.save(restaurant);
        if (save) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("新增失败");
        }

    }

    @PutMapping("update")
    public AjaxResult update(@RequestPart(value = "restaurantPic", required = false) MultipartFile restaurantPic,
                             @RequestPart("restaurant") RestaurantVM restaurantVM,
                             @RequestHeader(value = "token", required = false) String token) throws Exception {
        Restaurant restaurant = new Restaurant();
        BeanUtil.copyProperties(restaurantVM, restaurant);
        restaurant.setOpeningTime(DateUtil.parse(restaurantVM.getOpeningTime(), "HH:mm"));
        restaurant.setClosingTime(DateUtil.parse(restaurantVM.getClosingTime(), "HH:mm"));
        if (ObjectUtils.isNotEmpty(restaurantPic) && !restaurantPic.isEmpty()) {
            // 更新头像
            String avatar = FileUploadUtils.upload(RCConfig.getAvatarPath(), restaurantPic, MimeTypeUtils.IMAGE_EXTENSION);
            restaurant.setAvatar(avatar);
        }
//        restaurant.setUpdateBy(String.valueOf(jwtHelper.getUserId(token)));
//        restaurant.setUpdateTime(DateUtils.getNowDate());
        boolean update = restaurantService.updateById(restaurant);
        if (update) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @PutMapping("updateStatus")
    public AjaxResult updateState(@RequestBody RestaurantVM restaurantVM) throws Exception {
        Restaurant restaurant = new Restaurant();
        BeanUtil.copyProperties(restaurantVM, restaurant);
        if (restaurantService.updateById(restaurant)) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    // 删除食堂信息
    @DeleteMapping("{ids}")
    public AjaxResult delete(@PathVariable List<Long> ids) {
        boolean remove = restaurantService.removeByIds(ids);
        if (remove) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


}
